//
//  WelcomeViewController.swift
//  波波微博
//
//  Created by wave on 15/7/31.
//  Copyright © 2015年 wave. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomeViewController: UIViewController {

    //添加一个头部的底部约束
    private var iconBottomCons: NSLayoutConstraint?
    
    override func viewDidLoad() {
        super.viewDidLoad()


        setupImage()

        if let urlString = UserAccount.sharedUserAccount?.avatar_large{
            headImageView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
    }

    //在这里我们进行图片动画,因为label的约束设置的是对应头像的,所以移动头像就可以实现两个动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconBottomCons?.constant = UIScreen.mainScreen().bounds.height - 200
        
    
        //让头像和label实现上下滚动效果
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.view.layoutIfNeeded()
            
            }) { (Bool) -> Void in

            NSNotificationCenter.defaultCenter().postNotificationName(BBBSwitchMainInterfaceNotification, object: true)
        }
    }
    
    //将添加页面综合到一起
    private func setupImage(){
    
        view.addSubview(backIconView)
        view.addSubview(headImageView)
        view.addSubview(messageLabel)
        
        //设置自动布局
        //背景图片
        backIconView.ff_Fill(view)
        
        //头像
        headImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: headImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))

        view.addConstraint(NSLayoutConstraint(item: headImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
         view.addConstraint(NSLayoutConstraint(item: headImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        //以下两句话必须要在一起,所以图片约束添加到这里
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: headImageView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 160))
        iconBottomCons = view.constraints.last


        
        //label
        messageLabel.ff_AlignVertical(type: ff_AlignType.BottomCenter, referView: headImageView, size: nil, offset:CGPoint(x: 0, y: 16))
    }
    
   
    //MARK: - 懒加载控件
    //1. 背景图片
    private lazy var backIconView:UIImageView={
        
        let image = UIImageView(image: UIImage(named: "ad_background"))
    
        return image
    }()

    //2. 用户头像
    private lazy var headImageView:UIImageView = {
        
        let headImg = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        let str = UserAccount.sharedUserAccount?.avatar_large
        
        let url = NSURL(string: str!)
        
        headImg.sd_setImageWithURL(url)
        
        headImg.layer.masksToBounds = true
        
        headImg.layer.cornerRadius = 45
        
        return headImg
    }()
    
    //3. label
    private lazy var messageLabel : UILabel = {
        
        let label = UILabel()
        
        label.text = UserAccount.sharedUserAccount?.name
        
        label.sizeToFit()
        
        return label
    }()
}
